import * as SecureStore from 'expo-secure-store'
import { createSlice, PayloadAction } from '@reduxjs/toolkit';

interface Payment {
    id: string; // 每个订单唯一标识
    content: string;
    price: number;
    time: string;
    duration?: string | number | null;
}

interface PaymentState {
    paymentData: Payment[]; // 支付订单数组
    diamondCount: number;
}

const initialState: PaymentState = {
    paymentData: [],
    diamondCount: 0,
};

export interface RootState {
    payment: PaymentState;
}



const paymentSlice = createSlice({
    name: 'payment',
    initialState,
    reducers: {
        addPayment(state, action: PayloadAction<Payment>) {
            state.paymentData.push(action.payload); // 添加新的订单到数组中
            if (action.payload.content === '钻石') {
                const duration = typeof action.payload.duration === 'string' ? Number(action.payload.duration) : action.payload.duration;
                if (!isNaN(duration) && isFinite(duration)) {
                    state.diamondCount += duration; // 确保这里只加了 duration 的值，而不是其他值
                    SecureStore.setItemAsync('paymentData', JSON.stringify(state.paymentData));
                    SecureStore.setItemAsync('diamondCount', state.diamondCount.toString());
                    console.log('success', state.diamondCount, action.payload.duration);

                } else {
                    console.error('Invalid duration value:', action.payload.duration);
                }
            }
        },
        removePayment(state, action: PayloadAction<string>) {
            const idToRemove = action.payload;
            const index = state.paymentData.findIndex(order => order.id === idToRemove);
            if (index !== -1) {
                const removedOrder = state.paymentData[index];
                if (removedOrder.content === '钻石') {
                    const duration = removedOrder.duration;
                    const diamondsToRemove = typeof duration === 'string' ? Number(duration) : duration;
                    if (!isNaN(diamondsToRemove) && isFinite(diamondsToRemove)) {
                        state.diamondCount -= diamondsToRemove;
                        SecureStore.setItemAsync('paymentData', JSON.stringify(state.paymentData));
                        SecureStore.setItemAsync('diamondCount', state.diamondCount.toString());
                    } else {
                        console.error('Invalid duration value for removal:', removedOrder.duration);
                    }
                }
                state.paymentData.splice(index, 1);
            }
        },
        clearPayments(state) {
            state.paymentData = []; // 清空所有订单
            state.diamondCount = 0; // 同时清空钻石总数
            SecureStore.setItemAsync('paymentData', JSON.stringify(state.paymentData));
            SecureStore.setItemAsync('diamondCount', state.diamondCount.toString());
        },
        subtractDiamonds(state, action: PayloadAction<number>) {
            const amountToSubtract = Number(action.payload);
            if (amountToSubtract > 0 && state.diamondCount >= amountToSubtract) {
                state.diamondCount -= amountToSubtract;
                SecureStore.setItemAsync('diamondCount', state.diamondCount.toString());
            } else {
                console.error('Insufficient diamonds or invalid amount to subtract:', amountToSubtract);
            }
        },
    },
});



export const { addPayment, removePayment, clearPayments, subtractDiamonds } = paymentSlice.actions;
export default paymentSlice.reducer;


